package shortlink;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

import java.util.List;

@SpringBootTest
public class cloudtest {

    @Autowired
    DiscoveryClient discoveryClient;
//    负载均衡(集群模式，均匀访问多个端口)
    @Autowired
    LoadBalancerClient loadBalancerClient;


    @Test
    void discoverClientTest(){
        ServiceInstance choose = loadBalancerClient.choose("short-link-project");
        System.out.println(choose.getHost()+":"+choose.getPort());
        for (String serviceId : discoveryClient.getServices()) {
            System.out.println(serviceId);
            List<ServiceInstance> list= discoveryClient.getInstances(serviceId);
            for (ServiceInstance serviceInstance : list) {
                System.out.println(serviceInstance.getHost() + ":" + serviceInstance.getPort());
            }
        }

    }
}
